package com.condroid;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This is the StdByActivity for the application Condroid
 * provides the UI for the StdBySettings
 * @author Yasir Ali & Jun gao
 * Condroid Summer CSD 2011 - KTH
 */
public class StdbyActivity extends Activity {

	private Button helpButton;
	private Button commitButton;
	private EditText allowNoText;
	private EditText accessCodeText;
	private String showText;
	private TextView textView;
	public static String stdByFile;
	SharedPreferences stdByData;
	SharedPreferences.Editor AllowNo;
	SharedPreferences.Editor AllowAccess;
	private String[] buffer;
	private BroadcastReceiver smsReceiver;
	private IntentFilter smsFilter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.stdby);
		setUpViews();
	}

	/**
	 * This method is to set up View for the UI of StdbyActivity
	 * It has inside commit button and help button
	 * commit button calls the commitConfiguration methods which is
	 * in the Standbysetting class and that method saves configuration been
	 * made in the StdByActivity and calls the method commit configuration 
	 * to commit.
	 * Help button has an intent to move to StdByHhelpTopic Activity
	 * 
	 */
	
	private void setUpViews() {
		helpButton = (Button) findViewById(R.id.stdby_help);
		commitButton = (Button) findViewById(R.id.stdby_commit);
		allowNoText = (EditText) findViewById(R.id.edit_no_text);
		accessCodeText = (EditText) findViewById(R.id.edit_access_text);
		textView = (TextView)findViewById(R.id.stdby_id);
		showText = StdBySettings.getConfiguration(this);
		buffer=showText.split("/");
		allowNoText.setText(buffer[0]);
		accessCodeText.setText(buffer[1]);

		/**
		 * commit button to save configuration in shared preferences or a file
		 * it Commits the config and checks for the access code!!		
		 * it calls a method called commitConfiguration from standBySetting 
		 * class to save user settings.
		 **/
		
		commitButton.setOnClickListener(new View.OnClickListener() {
			private String allowData;
			private String accessCodeData;

			@Override
			public void onClick(View v) {
				allowData = allowNoText.getText().toString();
				accessCodeData = accessCodeText.getText().toString();
				if (StdBySettings
						.commitConfiguration(allowData, accessCodeData)) {
					if (ConstantData.consoleInUartThread != null){
						ConstantData.consoleInUartThread.finish();
						ConstantData.consoleInUartThread = null;
					}
					if (ConstantData.consoleOutStream != null) {
						try {
							ConstantData.consoleOutStream.close();
							ConstantData.consoleOutStream = null;
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					if (ConstantData.consoleUart != null
							&& ConstantData.consoleUartInfo != null) {
						IOIOThread.getInstance().closeUart(
								ConstantData.consoleUart,
								ConstantData.consoleUartInfo);
						ConstantData.consoleUart = null;
						ConstantData.consoleUartInfo = null;
					}
					smsReceiver = new SMSReceiver();
					smsFilter = new IntentFilter(
							"android.provider.Telephony.SMS_RECEIVED");
					ConstantData.smsReceiver = smsReceiver;
					ConstantData.smsFilter = smsFilter;
					ConstantData.appContext.registerReceiver(smsReceiver, smsFilter);
					ConstantData.mainActivity.finish();
					finish();
				} else
					makeToast("Configuration not saved, you must have "
							+ "used wrong format or Password!!");
			}
		});

		/**
		 * Help button takes the user to help topic activity where 
		 * the user can some useful information about how to use it.
		 **/
		
		helpButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(StdbyActivity.this,
						StdbyHelpTopicActivity.class);
				startActivity(intent);
			}
		});
	}

	/**
	 * Makes the toast or pops a window to display different msgs
	 * to the screen	
	 * @param text
	 */
	
	private void makeToast(String toast) {
		Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
	}

}